package com.dxy.controller;

import com.dxy.bean.*;
import com.dxy.mvc.ResponseBody;
import com.dxy.service.CourieService;
import com.dxy.service.impl.CourieServiceImpl;
import com.dxy.utils.DateFormatUtil;
import com.dxy.utils.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author DELL
 */
public class CourierController {
    CourieService courieService = new CourieServiceImpl();

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = courieService.console();
        Message msg = new Message();
        if(data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/courier/list.do")
    public String list (HttpServletRequest request ,HttpServletResponse response){
        //1. 获取查询数据的起始索引值
        int offset  = Integer.parseInt(request.getParameter("offset"));
        //2.    获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.    进行查询
        List<Courier> list = courieService.findByUserAll(true,offset,pageNumber);
        List<BootStrapTableCourier> list2 = new ArrayList<>();
        for (Courier courier : list) {
            String registeredTime = DateFormatUtil.format(courier.getRegisteredTime());
            String loginTime = courier.getLoginTime() == null ? "未登录" : DateFormatUtil.format(courier.getLoginTime());
            String status = courier.getStatus() == 0 ? "离线" : "在线";
            BootStrapTableCourier bootStrapTableCourier = new BootStrapTableCourier(courier.getId(), courier.getUsername(), courier.getUserPhone(), courier.getPassword(), courier.getIcCard(),courier.getSendTheNumber(), registeredTime, loginTime, status);
            list2.add(bootStrapTableCourier);
        }
        List<Map<String,Integer>> console =courieService.console();
        Integer total = console.get(0).get("data1_size");
        //  4.将集合封装为bootStrap - table识别的格式
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    /***
     * 根据手机号查询用户信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        //接收参数
        String userPhone = request.getParameter("userPhone");
        List<Courier> couriers =  courieService.findByUserPhone(userPhone);
        Message msg = new Message();

        if(couriers.size()==0){
            msg.setStatus(-1);
            msg.setResult("用户不存在");

        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(couriers);


        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        //1. 接收参数
        int id =Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String userPhone = request.getParameter("userPhone");
        String icCard = request.getParameter("icCard");
        Courier newCourier = new Courier();
        newCourier.setUsername(username);
        newCourier.setUserPhone(userPhone);
        newCourier.setIcCard(icCard);
        newCourier.setPassword(password);
        //2.  调用userService修改信息
        boolean flag = courieService.update(id, newCourier);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");

        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String icCard = request.getParameter("icCard");
        String password = request.getParameter("password");
        Courier courier = new Courier(username,userPhone,icCard,password);
        boolean flag = courieService.insert(courier);
        Message msg = new Message();
        if(flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("添加成功!");
        }else{
            //录入失败
            msg.setStatus(-1);
            msg.setResult("添加失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        // 获取请求参数
        int id = Integer.parseInt(request.getParameter("id"));
        // 调用userService.delete方法根据id删除
        boolean flag = courieService.delete(id);
        // 创建消息
        Message msg = new Message();
        if(flag){
            //删除成功返回
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            //删除失败
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}